home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / CRS / crs56.d81 / graph.sfx / graph.c next >
Text File  |  1990-02-12  |  9KB  |  343 lines

  1. /* GRAPHIC.C */
  2.  
  3. /*****************************************************************************/
  4. /*                    ┬ASIC ╔╧ ROUTINES FOR THE ╓─├ CHIP                     */
  5. /*                                                                           */
  6. /*  ╘HE FOLLOWING ├128 ROUTINES WERE USED TO SPEED UP OPERATIONS TO THE ╓─├  */
  7. /*                                                                           */
  8. /*  ┬┴╬╦  ┴──╥┼╙╙   ╬┴═┼                                                     */
  9. /* ▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐ */
  10. /*  15    $├──┴   ╥┼┴─╥┼╟       ╥EAD A VALUE FROM ╓─├ REG # IN ╪, VALUE IS   */
  11. /*                              IS RETURNED IN ┴.                            */
  12. /*                                                                           */
  13. /*  15    $├─├├   ╫╥╔╘┼╥┼╟      ╫RITE A VALUE TO ╓─├ REG # IN ┴, VALUE IS IN */
  14. /*                              ╪ REG.                                       */
  15. /*                                                                           */
  16. /*  15    $├┼0├   ╔╬╔╘80        ╥ESET ╓─├ TO DEFAULT CONDITIONS AND LOAD     */
  17. /*                              STANDARD ├OMMODORE CHARACTER SET.            */
  18. /*                                                                           */
  19. /*  15    $├──8   ╥┼┴─80        ╥EADS A MENORY LOCATION FROM ╓─├ MEMORY, SET */
  20. /*                              ADDRESS TO BE READ IN ╓─├ REG 18 & 19,       */
  21. /*                              VALUE RETURNED IN ┴.                         */
  22. /*                                                                           */
  23. /*  15    $├─├┴   ╫╥╔╘┼80       ╫RITE A VALUE TO ╓─├'S MEMORY, SET MENORY    */
  24. /*                              ADDRESS IN ╓─├ REG 18 & 19, VALUE IN ┴       */
  25. /*                                                                           */
  26. /*****************************************************************************/
  27.  
  28.  
  29.  
  30. READVDC(PORTREG) /* READ A VDC. REG. */ CHAR PORTREG;
  31. CHAR PORTREG,A,Y;
  32.     SYS(0X0F,0XCDDA,&A,&PORTREG,&Y);
  33.     RETURN(A);   
  34.  
  35. WRITEVDC(PORTREG,REGBYTE) CHAR PORTREG,REGBYTE;
  36. CHAR Y;
  37.  
  38. *   SYS(0X0F,0XCDCC,®BYTE,&PORTREG,&Y);
  39.  
  40. POKEVDC(ADDR,BYTE) CHAR BYTE; UNSIGNED ADDR;
  41. CHAR HI,LO,PORTREG,REGBYTE,Y;
  42.  
  43.     HI = ADDR;
  44.     LO = ADDR >> 8;
  45.     PORTREG = 18;
  46.     REGBYTE = HI;
  47.     SYS(0X0F,0XCDCC,®BYTE,&PORTREG,&Y);
  48.     PORTREG = 19;
  49.     REGBYTE = LO;
  50.     SYS(0X0F,0XCDCC,®BYTE,&PORTREG,&Y);
  51.   ) SYS(0X0F,0XCDCA,&BYTE,&PORTREG,&Y);
  52.  
  53. PEEKVDC(ADDR) UNSIGNED ADDR;
  54. CHAR HI,LO,REGBYTE,PORTREG,Y;
  55.  
  56.     HI = ADDR;
  57.     LO = ADDR >> 8;
  58.     PORTREG = 18;
  59.     REGBYTE = HI;
  60.     SYS(0X0F,0XCDCC,®BYTE,&PORTREG,&Y);
  61.     PORTREG = 19;
  62.     REGBYTE = LO;
  63.     SYS(0X0F,0XCDCC,®BYTE,&PORTREG,&Y);
  64.     SYS(0X0F,0XCDD8,®BYTE,&PORTREG,&Y); 
  65.     RETURN (REGBYTE);
  66.  
  67. /*****************************************************************************/
  68. /*         ╥OUTINES TO ENABLE AND DISABLE THE HIRES MODE OF THE ╓─├          */
  69. /*                                                                           */
  70. /*****************************************************************************/
  71.  
  72. HIRES() /* INIT HIRES MODE */
  73.     WRITEVDC(25,135);
  74.     WRITEVDC(26,113); 
  75.     WRITEVDC(10,/);
  76. HIRESCOLOR(FOREGROUND) CHAR BACKGROUND;
  77. /* THE BACKGROUND IS ALLWAYS BLACK */
  78.     WRITEVDC(26,(FOREGROUND * 16));
  79. CLRHIRES(FLAG)
  80. INT FLAG;
  81. CHAR BYTE;
  82. INT I;
  83.  
  84.     WRITEVDC(18,0);
  85.     WRITEVDC(19,0);
  86.     IF (FLAG) BYTE = 0;
  87.     ELSE BYTE = 0XFF;
  88.     FOR (I=0 ; I<=64 ; I++) █
  89.         WRITEVDC(31,BYTE);
  90.         WRITEVDC(30,0); 
  91.     ▌
  92.  
  93. TEXTMODE(BANK,CHARADDR) UNSIGNED CHARADDR; INT BANK;
  94.     WRITEVDC(25,71);
  95.     CLRHIRES(-1);
  96.     WRITEVDC(10,96);
  97.     LOADCHAR(BANK,CHARADDR); /* C POWER'S CHARACTER SET IS AT $3800 6/
  98.  
  99. LOADCHAR(FLAG,ADDR) INT BANK; UNSIGNED ADDR;
  100. CHAR A,X,Y;
  101. INT ENDING,I,J;
  102.  
  103.     SYS(0X0F,0XCE0C,&A,&X,&Y); /* USE ╥╧═ ROUTINE ╔╬╔╘80 TO LOAD COMMODORE CHAR. SET */
  104.     IF (BANK) █
  105.          ENDING = ADDR+2048;
  106.          WRITEVDC(18,32);
  107.          WRITEVDC(19,0);
  108.          FOR (I=ADDR ; I<=ENDING ; I=I+8) █
  109.              FOR (J=0 ; J<=7 ; J++) █
  110.                  WRITEVDC(31,PEEK(15,I+J));
  111.              ▌ 
  112.              FOR (J=8 ; J<=15 ; J++) █
  113.                  WRITEVDC(31,0);
  114.              ▌ 
  115.          ▌
  116.     ▌ 
  117.  
  118. /******************************************************************************/
  119. /*               ╨LOTING AND DRAWING ROUTINES FOR THE ╓─├                     */
  120. /*                                                                            */
  121. /* ╘HESE WOULD BE MUCH FASTER IF WRITTEN IN ═.╠., BUT ARE ╧.╦. FOR DOING      */
  122. /* DRAWINGS, BUT WOULDN'T DO FOR GAMES.                                       */
  123. /*                                                                            */
  124. /******************************************************************************/
  125.  
  126. PLOT(SX,SY,MODE) INT SX,SY,MODE;
  127. INT VDCADDR,A,X,Y,MFLAG;
  128. EXTERN INT CURRENTX,CURRENTY;
  129. CHAR MASKBYTE,VDCBYTE,ADDRLO,ADDRHI,PORTREG,REGBYTE;
  130.  
  131. STATIC INT MASK[8] = █ 128,64,32,16,8,4,2,1 ▌;
  132.     VDCADDR = SY*80+SX/8;
  133.     MASKBYTE = MASK[(SX % 8)];
  134.     ADDRHI = VDCADDR >> 8;
  135.     ADDRLO = VDCADDR;
  136.     PORTREG = 18;
  137.     REGBYTE = ADDRHI;
  138.     SYS(0XF,0XCDCC,®BYTE,&PORTREG,&Y);
  139.     PORTREG = 19;
  140.     REGBYTE = ADDRLO;
  141.     SYS(0XF,0XCDCC,®BYTE,&PORTREG,&Y);
  142.     SYS(0XF,0XCDD8,&VDCBYTE,&X,&Y);
  143.     PORTREG = 18;
  144.     REGBYTE = ADDRHI;
  145.     SYS(0XF,0XCDCC,®BYTE,&PORTREG,&Y);
  146.     PORTREG = 19;
  147.     REGBYTE = ADDRLO;
  148.     SYS(0XF,0XCDCC,®BYTE,&PORTREG,&Y);
  149.     IF (MODE = 1)█
  150.         REGBYTE = (VDCBYTE  MASKBYTE);
  151.         GOTO PLOTTER;
  152.     ▌
  153.     IF (MODE = 2) █
  154.         REGBYTE = (VDCBYTE & (MASKBYTE));
  155.         GOTO PLOTTER;
  156.     ▌
  157.     IF (MODE = 0 )█
  158.         MASKBYTE = MASKBYTE;
  159.         REGBYTE = (VDCBYTE & MASKBYTE);
  160.         GOTO PLOTTER;
  161.     ▌
  162.     IF (MODE>2  MODE<0)
  163.         RETURN(0);
  164.  
  165.     PLOTTER:
  166.  
  167.     SYS(0XF,0XCDCA,®BYTE,&PORTREG,&Y); 
  168.     RETURN(1);
  169.  
  170. DRAW(X1,Y1,X2,Y2,MODE) INT X1,X2,Y1,Y2,MODE;
  171. EXTERN CURRENTX,CURRENTY;
  172. INT DX,DY,ADX,ADY,XA,YA,D,INC1,INC2;
  173.  
  174.     PLOT(X1,Y1);
  175.     DX = X2-X1;
  176.     DY = Y2-Y1;
  177.  
  178.     IF(DX<0) █
  179.         XA = -1;
  180.         ADX = -DX;
  181.     ▌ ELSE █
  182.         XA = 1;
  183.         ADX = DX;
  184.     ▌
  185.     IF(DY<0) █
  186.         YA = -1;
  187.         ADY = -DY;
  188.     ▌ ELSE █
  189.         YA = 1;
  190.         ADY = DY;
  191.     ▌
  192.  
  193.     IF(ADY>ADY) █
  194.         INC1 = ADY << 1;
  195.         D = INC1 - ADX;
  196.         INC2 = INC1 - (ADX << 1);
  197.         
  198.         WHILE(X1 != X2) █
  199.             X1 += XA;
  200.             IF(D<0)
  201.                 D += INC1;
  202.             ELSE █
  203.                 Y1 += YA;
  204.                 D += INC2;
  205.             ▌
  206.             PLOT(X1,Y1);
  207.         ▌
  208.     ▌ ELSE █
  209.         INC1 = ADX << 1;
  210.         D = INC1 - ADY;
  211.         INC2 = INC1 - (ADY << 1);
  212.  
  213.         WHILE(Y1 != Y2) █
  214.             Y1 += YA;
  215.             IF(D<0)
  216.                 D += INC1;
  217.             ELSE █
  218.                 X1 += XA;
  219.                 D += INC2;
  220.             ▌
  221.             PLOT(X1,Y1);
  222.         ▌
  223.     ▌
  224.     CURRENTX = X2;
  225.     CURRENTY = Y2;
  226.  
  227. DRAWTO(X,Y,MODE) INT X,Y,MODE;
  228. EXTERN INT CURRENTX,CURRENTY;
  229.  
  230.     DRAW(CURRENTX,CURRENTY,X,Y,MODE);
  231.     CURRENTX = X;
  232.     CURRENTY =Y;
  233.  
  234. DRAWREL(DX,DY,MODE) INT DX,DY,MODE;
  235. EXTERN CURRENTX,CURRENTY;
  236.     
  237.     DRAW(CURRENTX,CURRENTY,CURRENTX+DX,CURRENTY+DY,MODE);
  238.     CURRENTX += DX;
  239.     CURRENTY += DY;
  240.  
  241. HLINE(LENGTH,MODE) INT LENGTH,MODE;
  242. /* DRAWS A HORZ. LINE FRON THE CURRENT ╪ ┘ COORDS */
  243. EXTERN INT CURRENTX,CURRENTY;
  244. INT I,END;
  245.  
  246.     END =CURRENTX+LENGTH;
  247.     IF (LENGTH >0 )
  248.        FOR (I=CURRENTX ; I<= END ; I++) PLOT(I,CURRENTY,MODE);
  249.     ELSE 
  250.         FOR (I=END ; I<= CURRENTX ; I++) PLOT(I,CURRENTY,MODE);
  251.     CURRENTX = END;    
  252.  
  253. VLINE(LENGTH,MODE) INT LENGTH,MODE;
  254. /* DRAWS A VERTICAL LINE FROM THE CURRENT ╪ ┘ COORDS */
  255. EXTERN INT CURRENTX,CURRENTY;
  256. INT I,END;
  257.  
  258.     END = CURRENTY+LENGTH;
  259.     IF(LENGTH>0)
  260.         =OR(I=CURRENTY ; I<=END ; I++) PLOT(CURRENTX,I,MODE);
  261.     ELSE
  262.         FOR(I=END ; I<=CURRENTY ; I++) PLOT(CURRENTX,I,MODE);
  263.     CURRENTY = END;    
  264.  
  265. BOX(X,Y,X1,Y1,MODE) INT X,Y,X1,Y1,MODE;
  266. /* DRAWS A BOX OF H & W FROM CURRENT ╪ ┘ COORDINATES */
  267.     DRAW(X,Y,X1,Y,MODE);
  268.     DRAW(X1,Y,X1,Y1,MODE);
  269.     DRAW(X1,Y1,X,Y1,MODE);
  270. R   DRAW(X,Y1,X,Y,MODE);
  271.  
  272. SCALE(Y) INT Y;
  273. /* MAP Y COORDINATE IN TO 640 ╪ 450 SCREEN */
  274.     IF (Y<=449)█ 
  275.         Y = 449-Y;
  276.         RETURN(Y*20/45);
  277.     ▌
  278.     ELSE
  279.         RETURN(-1);
  280.  
  281. /**********************************************1******************************/
  282. /*                 ├URSOR LOCATION ROUTINES FOR HIRES MODE                   */
  283. /*                                                                           */
  284. /*  ├URRENT LOCATION IS IN THE GLOBAL VARIABLES CURRENTX & CURRENTY, DRAWTO, */
  285. /*  DRAWREL,HLINE,VLINE AND BOX ALL USE THESE VARIABLES. ─RAW SETS CURRENT   */
  286. /*  ╪ & ┘ TO IT'S LAST COORDINATES. ╨LOT DOSEN'T CHANGE CURRENTX OR CURRENTY */
  287. /*  OR  REFERENCE THEM.                                                      */
  288. /*                                                                           */
  289. /*****************************************************************************/
  290.  
  291. WHEREX()
  292. EXTERN INT CURRENTX;
  293.     RETURN(CURRENTX);
  294.  
  295. WHEREY()
  296. EXTERN INT CURRENTY;
  297.     RETURN(CURRENTY);
  298.  
  299. GOTOXY(X,Y) INT X,Y;
  300. EXTERN INT CURRENTX,CURRENTY;
  301.     CURRENTX = X;
  302.     CURRENTY = Y;
  303.